/*
* Created by: yyf
* Description: 基金管理--拟设立基金--基金合伙人详情
*/
<template>
    <div class="ca-box-border-top-none headTitle">
        <!--合伙人头部-->
        <div>
            <el-row class="ca-m-b-20">
                <el-col :span="14" class="ca-title">
                    <h3 class="ca-title-left">LP情况：</h3>
                    <el-select v-model="fundPartnerId" @change="changeFundPartnerType" placeholder="请选择" style="width:200px">
                        <el-option
                            v-for="item in fundPartnerOptions"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-col>
                <el-col :span="10" class="ca-pull-right ca-align-right">
                    <el-button type="primary" @click="addDialog = true" :disabled="!isAuthPartnerEdit">添加</el-button>
                </el-col>
            </el-row>
        </div>
        <!--合伙人详情-->
        <div>
            <el-row v-if="!fundPartnerData.length" class="ca-data-font">暂无数据</el-row>
            <el-row
                v-if="fundPartnerData.length"
                v-for="(item, index) in fundPartnerData"
                :key="item.fundAnsatPartnerEntity.id"
                class="ca-m-t-10 ca-m-b-20 ca-box">
                <div v-if="!item.isEdit">
                    <el-row class="ca-border-b ca-partner-padding">
                        <el-row>
                            <el-col :span="12" class="ca-title">
                                <h3>LP：{{item.fundAnsatPartnerEntity.lpName}}</h3>
                            </el-col>
                            <el-col :span="4" class="pull-right ca-align-right">
                                <el-button type="text" class="ca-edit-btn el-icon-edit" @click="changeEdit(item)" :disabled="!isAuthPartnerEdit">编辑</el-button>
                            </el-col>
                        </el-row>
                        <el-row class="ca-partner-title">
                            <el-col :span="6">
                                <div class="ca-title-con"><span class="ca-smallTit">投资状态：</span>{{item.fundAnsatPartnerEntity.state | getNameById('ansatLp')}}</div>
                            </el-col>
                            <el-col :span="6">
                                <div class="ca-title-con"><span class="ca-smallTit">拟投金额：</span>{{item.fundAnsatPartnerEntity.investAmount | format1}}万元</div>
                            </el-col>
                            <el-col :span="6">
                                <div class="ca-title-con"><span class="ca-smallTit">服务人：</span>{{item.fundAnsatPartnerEntity.serviceMan}}</div>
                            </el-col>
                            <el-col :span="6">
                                <div class="ca-title-con"><span class="ca-smallTit">记录时间：</span>{{$moment(item.fundAnsatPartnerEntity.createTime).format("YYYY-MM-DD")}}</div>
                            </el-col>
                        </el-row>
                    </el-row>
                    <el-row class="ca-partner-padding ca-partner-title">
                        <h3>募资记录：</h3>
                        <span style="word-break: break-all">{{item.fundAnsatPartnerEntity.record}}</span>
                    </el-row>
                    <el-row class="ca-partner-padding ca-partner-title">
                        <h3>相关文档：</h3>
                        <div class="ca-file-ul">
                            <el-row class="ca-file-row" v-for="(file, detailIndex) in item.fileEntityList" :key="detailIndex">
                                <el-col :span="5">
                                    <div class="ca-text-over-flow-ellipsis">{{file.fileType | getNameByType  }}</div>
                                </el-col>
                                <el-col :span="1">
                                    <tz-icon-file-type class="ca-m-r-5" :fileType="file.fileName"></tz-icon-file-type>
                                </el-col>
                                <el-col :span="15">
                                    <div class="ca-text-over-flow-ellipsis ca-pointer"
                                         @click="previewFileCheck(file.id, file.fileName)">
                                        <span class="ca-font-size-16 ca-pointer">{{file.fileName}}</span>
                                    </div>
                                </el-col>
                                <el-col :span="3" class="ca-pull-right ca-align-right">
                                    <a class="ca-download ca-m-r-10" @click="downloadFile(file.id)"></a>
                                </el-col>
                            </el-row>
                        </div>
                    </el-row>
                </div>
                <div v-if="item.isEdit">
                    <el-form label-width="90px" label-position="right" :rules="rules" ref="partnerForm" :model="item.fundAnsatPartnerEntity">
                        <el-row class="ca-leng-30 ca-border-b ca-p-tb-lr-9-15">
                            <el-row class="ca-p-10">
                                <el-col class="pull-right ca-align-right">
                                    <el-button type="danger" size="medium" @click="delPartnerItem(item, index)">删除</el-button>
                                    <el-button type="primary" size="medium" @click="editPartnerSave(item, index)">保存</el-button>
                                    <el-button size="medium" @click="close(item, index)">取消</el-button>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="潜在LP">
                                        <el-select v-model="item.fundAnsatPartnerEntity.lpId" @change="changeLp(item)" filterable style="width:70%">
                                            <el-option
                                                v-for="guestItem in guest"
                                                :key="guestItem.guestId"
                                                :label="guestItem.guestName"
                                                :value="guestItem.guestId">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="投资状态">
                                        <el-select v-model="item.fundAnsatPartnerEntity.state" placeholder="请选择" style="width:70%">
                                            <el-option
                                                v-for="stateItem in fundPartnerOptionsAdd"
                                                :key="stateItem.id"
                                                :label="stateItem.name"
                                                :value="stateItem.id">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="服务人">
                                        <el-input v-model="item.fundAnsatPartnerEntity.serviceMan" :maxlength="100" style="width:70%"></el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="拟投资金额" prop="investAmount">
                                        <el-input v-model="item.fundAnsatPartnerEntity.investAmount" placeholder="万元" :maxlength="21" style="width:70%"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-row>
                        <el-row class="ca-partner-padding ca-partner-title">
                            <h3>募资记录：</h3>
                            <el-input type="textarea" v-model="item.fundAnsatPartnerEntity.record" placeholder="最多输入3000字" :maxlength="3000"></el-input>
                        </el-row>
                        <el-row class="ca-partner-padding ca-partner-title">
                            <h3 class="ca-m-b-10">相关文档：
                                <div class="ca-inline-block ca-m-r-10 ">
                                    <csUpload
                                        :fileType="fileType"
                                        :businessId="item.fundAnsatPartnerEntity.id"
                                        :isShowFileChildType="false"
                                        uploadType="all"
                                        @getFileList="getPartnerItemDetail(item, index, true)"
                                    ></csUpload>
                                </div>
                            </h3>
                            <div class="ca-file-ul">
                                <el-row class="ca-file-row" v-for="(file, editIndex) in item.fileEntityList" :key="editIndex">
                                    <el-col :span="5">
                                        <div class="ca-text-over-flow-ellipsis">{{file.fileType | getNameByType  }}</div>
                                    </el-col>
                                    <el-col :span="1">
                                        <tz-icon-file-type class="ca-m-r-5" :fileType="file.fileName"></tz-icon-file-type>
                                    </el-col>
                                    <el-col :span="15">
                                        <div class="ca-text-over-flow-ellipsis ca-pointer"
                                             @click="previewFileCheck(file.id, file.fileName)">
                                            <span class="ca-font-size-16 ca-pointer">{{file.fileName}}</span>
                                        </div>
                                    </el-col>
                                    <el-col :span="3" class="ca-pull-right ca-align-right">
                                        <a class="ca-download ca-m-r-10" @click="downloadFile(file.id)"></a>
                                        <el-button class="ca-table-btn" @click="deleteFile(file.id, editIndex, index)">
                                            <i class="fa fa-times"></i>
                                        </el-button>
                                    </el-col>
                                </el-row>
                            </div>
                        </el-row>
                    </el-form>
                </div>
            </el-row>
        </div>
        <csAddPartner
            :addDialog="addDialog"
            :partnerEntity="partnerEntity"
            :fileDetail="fileDetail"
            @cancelDialog="cancelDialog"
            @addFundPartner="addFundPartner"
            @deleteFileItem="deleteFileItem"
            @getFileUploadedReturnDataFun="getFileUploadedReturnDataFun">
        </csAddPartner>
    </div>
</template>
<script type="text/ecmascript-6">
    import {api} from '../api';
    import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
    import mined from '__components/file-list/mixin';
    import { isNoAuth } from '__service/utils';
    export default {
        mixins: [mined],
        data() {
            var validateNumber = (rule, value, callback) => {
                var reg = /^\d{1,10}(\.\d{1,10})?$/;
                if (value && (!reg.test(value))) {
                    callback(new Error('请输入正确范围的数字'));
                } else {
                    callback();
                }
            };
            return {
                itemId: this.$route.params.itemId,
                isAuthEdit: false,
                fundPartnerData: [],// 合伙人
                fundPartnerId: '',// 搜索参数
                guest: this.$store.state.user.guest,
                fundPartnerOptions: [{id: '', name: '全部'},...this.$store.state.user.dictionaryMap.ansatLp],// 筛选
                fundPartnerOptionsAdd: this.$store.state.user.dictionaryMap.ansatLp,// 弹框筛选
                addDialog: false,// 添加弹框开关
                fileList: [],
                fileDetail: [],
                partnerEntity: {
                    fundId: this.$route.params.itemId,
                    id: '',
                    investAmount: null,
                    lpId: '',
                    lpName: '',
                    record: '',
                    serviceMan: this.$store.state.user.cnName,
                    state: '1',
                },
                fileType: this.$store.state.user.fileFloderDicVoList[48].type,// 基金文件类型
                isEditList: [],// 编辑列表
                isAuthPartnerEdit: isNoAuth('拟设立基金-合伙人信息-编辑'),
                rules: {
                    investAmount: [
                        { validator: validateNumber, trigger: 'blur' }
                    ],
                },
            }
        },
        props: {
        },
        watch: {
        },
        mounted() {
            this.getPartnerDetail();
        },
        methods: {
            // 文件成功返回的数据
            getFileUploadedReturnDataFun(data) {
                let fileItem = data.map(item=>{
                    return item.id;
                })
                this.fileList = this.fileList.concat(fileItem)
                this.fileDetail = this.fileDetail.concat(data);
            },
            // 删除文件
            deleteFile(id, editIndex, index) {
                this.$confirm('确认删除此文件?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    showClose: false
                }).then(async () => {
                    let resData = await apiHttp(api.DELETE_FILE, {id: id});
                    if (resData.resCode === STATUS_HTTP_SUCCESS) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!',
                            center: true
                        });
                        this.fundPartnerData[index].fileEntityList.splice(editIndex, 1);
                    }
                })
            },
            // 获取合伙人列表
            async getPartnerDetail() {
                let partnerParam = {
                    fundId: this.$route.params.itemId,
                    state: this.fundPartnerId
                }
                let {resCode, data} = await apiHttp(api.ESTABLISHING_FUNDS_PARTNER_LIST, partnerParam);
                if (resCode === STATUS_HTTP_SUCCESS) {
                    this.fundPartnerData = data;
                    this.fundPartnerData.map((item, index)=>{
                        this.$set(this.fundPartnerData[index], 'isEdit', false)
                    })
                }
            },
            // 获取合伙人详情
            async getPartnerItemDetail(item, index, val) {
                let {resCode, data} = await apiHttp(api.ESTABLISHING_FUNDS_PARTNER_DETAIL, {id: item.fundAnsatPartnerEntity.id});
                if (resCode === STATUS_HTTP_SUCCESS) {
                    let newFundPartner = {
                        fundAnsatPartnerEntity: data.fundAnsatPartnerEntity,
                        fileEntityList: data.fileEntityList,
                        isEdit: val
                    }
                    this.fundPartnerData.splice(index, 1, newFundPartner)
                }
            },
            // 改变潜在LP时赋值
            changeLp(item) {
                this.guest.map(guestItem=>{
                    if (guestItem.guestId === item.fundAnsatPartnerEntity.lpId) {
                        item.fundAnsatPartnerEntity.lpName = guestItem.guestName;
                    }
                })
            },
            // 合伙人列表根据state类型筛选
            changeFundPartnerType(val) {
                this.fundPartnerId = val;
                this.getPartnerDetail();
            },
            // 添加合伙人
            async addFundPartner() {
                if (this.partnerEntity.lpName) {
                    this.partnerEntity.investAmount = Number(this.partnerEntity.investAmount);
                    let {resCode} = await apiHttp(api.ESTABLISHING_FUNDS_PARTNER_ADD, {
                        fileList: this.fileList,
                        fundAnsatPartnerEntity: this.partnerEntity
                    });
                    if (resCode === STATUS_HTTP_SUCCESS) {
                        this.$message({
                            type: 'success',
                            message: '添加成功！',
                            center: true
                        })
                        this.$emit('addRedDot', 2);
                        this.cancelDialog();
                        this.getPartnerDetail();
                        this.fileList = [];
                    }
                } else {
                    this.$message({
                        type: 'error',
                        message: '请选择LP名称！',
                        center: true
                    })
                }
            },
            // 进入单个编辑页
            changeEdit(item) {
                item.isEdit = true;
            },
            // 删除合伙人
            async delPartnerItem(item, index) {
                this.$confirm('确认删除此文件?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    showClose: false
                }).then(async()=>{
                    let {resCode} = await apiHttp(api.ESTABLISHING_FUNDS_PARTNER_DEL, {id: item.fundAnsatPartnerEntity.id});
                    if (resCode === STATUS_HTTP_SUCCESS) {
                        item.isEdit = false;
                        this.$message({
                            type: 'success',
                            message: '删除成功！',
                            center: true
                        })
                        this.fundPartnerData.splice(index, 1)
                    }
                })
            },
            // 关闭编辑
            async close(item, index) {
                this.getPartnerItemDetail(item, index, false)
            },
            // 修改合伙人
            async editPartnerSave(item, index) {
                this.$refs.partnerForm[0].validate(async (valid) => {
                    if (valid) {
                        let {resCode} = await apiHttp(api.ESTABLISHING_FUNDS_PARTNER_EDIT, item.fundAnsatPartnerEntity);
                        if (resCode === STATUS_HTTP_SUCCESS) {
                            this.$message({
                                type: 'success',
                                message: '修改成功！',
                                center: true
                            })
                            this.$emit('addRedDot', 2);
                            this.getPartnerItemDetail(item, index, false);
                        }
                    }
                })
            },
            // 删除添加弹出框的文件
            deleteFileItem(index) {
                this.fileList.splice(index, 1);
                this.fileDetail.splice(index, 1);
            },
            // 清空弹框
            cancelDialog() {
                this.addDialog = false;
                this.partnerEntity = {
                    fundId: this.$route.params.itemId,
                    id: '',
                    investAmount: null,
                    lpId: '',
                    lpName: '',
                    record: '',
                    serviceMan: this.$store.state.user.cnName,
                    state: '1'
                }
                this.fileDetail = [];
            }
        },
        components: {
            'csUpload': require('__components/upload-btn/index'),//上传文件组件
            'csAddPartner': require('./add'),//上传文件组件
        }
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    .headTitle{
        font-size:16px;
        padding: 20px;
    }
    .ca-partner-title{
        h3 {
            font-weight:normal;
            font-size: 18px;
            line-height: 40px;
        }
        span {
            font-size: 14px;
        }
    }
    .ca-file-ul {
        margin-top: 0;
    }
    .ca-data-font {
        color: #999;
        margin-bottom: 20px;
    }
    .ca-partner-padding {
        padding: 10px 20px;
    }
    .ca-smallTit {
        color: #999;
    }
    .ca-title-con {
        width: 100%;
        word-break: break-all;
    }
    .ca-title-left {
        width: 90px;
        float: left;
    }
</style>
